/**
 * 
 */
package rs.fon.rapidminer.operator.learner;

import java.util.List;
import java.io.*;

import rs.fon.rapidminer.parameter.ParameterTypeProcessFile;
import rs.fon.rapidminer.process.builder.generictree.GenericTreeProcessBuilder;
import rs.fon.rapidminer.serialization.ProcessDecoder;

import com.rapidminer.example.ExampleSet;
import com.rapidminer.operator.Model;
import com.rapidminer.operator.OperatorDescription;
import com.rapidminer.operator.OperatorException;
import com.rapidminer.operator.learner.LearnerCapability;
import com.rapidminer.operator.learner.tree.Tree;
import com.rapidminer.operator.learner.tree.TreeModel;
import com.rapidminer.parameter.ParameterType;

/**
 * This class is RapidMiner integration point. It redefines learn method in
 * order to build it's own {@link Model} and it uses it's parameter type to
 * obtain information form gui and pass it to learn method.
 * 
 * @author Nikola Nikolic
 * 
 */
public class GenericTreeModelLearner extends ModelLearner {

	private static String PROCESS_FILE = "process file";

	public GenericTreeModelLearner(OperatorDescription description) {
		super(description);
		// TODO Auto-generated constructor stub
	}

	@Override
	public Model learn(ExampleSet eSet) throws OperatorException {
		ExampleSet exampleSet = (ExampleSet)eSet.clone();
		
//		GenericTreeProcessBuilder gtpb = new GenericTreeProcessBuilder();
//		rs.fon.rapidminer.process.Process process = gtpb.buildProces();
		
		String fileLocation = getParameterAsString(PROCESS_FILE);
		
		File file = new File(fileLocation);
		rs.fon.rapidminer.process.Process process = ProcessDecoder.decodeFromXMLToProces(file.getAbsolutePath());
		
		rs.fon.rapidminer.operator.model.builder.TreeBuilder builder = new rs.fon.rapidminer.operator.model.builder.TreeBuilder(process);
		
		// learn tree
    	Tree root = builder.learnTree(exampleSet);
        
        // create and return model
        return new TreeModel(exampleSet, root);
	}

	//napraviti buildera	
	
	@Override
	public List<ParameterType> getParameterTypes() {

		List<ParameterType> types = super.getParameterTypes();
		ParameterType type = new ParameterTypeProcessFile(PROCESS_FILE,
				"Process file from which to load the generic tree", "pml",
				false, GenericTreeProcessBuilder.class);
		type.setExpert(false);
		types.add(type);
		return types;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.rapidminer.operator.learner.Learner#supportsCapability(com.rapidminer
	 * .operator.learner.LearnerCapability)
	 */
	public boolean supportsCapability(LearnerCapability capability) {
		// TODO Auto-generated method stub
		 if (capability == com.rapidminer.operator.learner.LearnerCapability.BINOMINAL_ATTRIBUTES)
	            return true;
	     if (capability == com.rapidminer.operator.learner.LearnerCapability.POLYNOMINAL_ATTRIBUTES)
	            return true;
	     if (capability == com.rapidminer.operator.learner.LearnerCapability.NUMERICAL_ATTRIBUTES)
	            return true;
	     if (capability == com.rapidminer.operator.learner.LearnerCapability.POLYNOMINAL_CLASS)
	            return true;
	     if (capability == com.rapidminer.operator.learner.LearnerCapability.BINOMINAL_CLASS)
	            return true;
	     if (capability == com.rapidminer.operator.learner.LearnerCapability.WEIGHTED_EXAMPLES)
	            return true;
	     if (capability == com.rapidminer.operator.learner.LearnerCapability.NUMERICAL_CLASS)
	    	    return true;
	    		 
	     return false;
	}

}
